﻿using System;
using UnityEngine;

public class ErrorMark : MonoBehaviour
{
    private static Camera cam;
    private Collider co;
    public string errorMessage;
    private GUISkin guiSkin;
    private bool isMouseOver;
    private static Rect rect = new Rect(0f, 0f, 400f, 30f);

    private void Awake()
    {
        this.co = base.GetComponent<Collider>();
        this.guiSkin = GameResources.GUISkin;
    }

    public static ErrorMark Create(string errorMessage, Vector3 position)
    {
        ErrorMark mark = GameResources.ErrorMark.CloneGameObject<ErrorMark>();
        mark.errorMessage = errorMessage;
        mark.transform.position = position;
        Debug.LogError(errorMessage);
        return mark;
    }

    private void OnGUI()
    {
        if (this.isMouseOver)
        {
            if (cam == null)
            {
                cam = Camera.main;
            }
            GUI.skin = this.guiSkin;
            Vector3 position = base.transform.position;
            Vector3 vector2 = cam.WorldToScreenPoint(position);
            vector2.y = Screen.height - vector2.y;
            rect.position = vector2;
            rect.x -= rect.width / 2f;
            rect = GUILayout.Window(this.GetHashCode(), rect, id => GUILayout.Label(this.errorMessage, new GUILayoutOption[0]), string.Empty, new GUILayoutOption[0]);
        }
    }

    private void Update()
    {
        if (cam == null)
        {
            cam = Camera.main;
        }
        if (cam != null)
        {
            RaycastHit hit;
            this.isMouseOver = this.co.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, float.PositiveInfinity);
        }
    }
}

